package abs.syn;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Dependency implements Cloneable{
	public int govIdx=-1, depIdx =-1;
	public String gov = null, dep = null;
	public String name = null;
//	public DepType name = null;
	public static final Pattern p = Pattern.compile("(.+?)\\((.+?)\\-(\\d+)'??, (.+?)\\-(\\d+)'??\\)");
	@Override public Object clone(){
		try {
			return super.clone();
		} catch (CloneNotSupportedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public Dependency(String string) {
		buildFromString(string);
	}
	
	public Dependency(){
		
	}

	public boolean buildFromString(String str){
//		nsubjpass(inhibited-9, Binding-1)
		Matcher m = p.matcher(str);
		if (m.find()){
			name = m.group(1);
			gov = m.group(2);
			dep = m.group(4);
			govIdx = Integer.parseInt(m.group(3));
			depIdx = Integer.parseInt(m.group(5));
			return true;
		}
		else{
			return false;
		}
	}
	
	public static List<Dependency> buildListFromString(String str){
		String[] lines = str.split("\n");
		ArrayList<Dependency> list = new ArrayList<Dependency>(lines.length);
		for (int i = 0; i < lines.length; i++){
			Dependency dep = new Dependency(lines[i]);
			if (dep.name==null)
				System.err.println("[DEP_PARSE] err "+lines[i]);
			else
				list.add(dep);
		}
		return list;
	}
	
	@Override public String toString(){
		return name+"("+gov+"-"+govIdx+", "+dep+"-"+depIdx+")";
	}
}
